﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Smile.Imaging
{
    public class Image : IImage
    {
        // @Properties
        public int Width
        {
            get 
            { 
                throw new NotImplementedException(); 
            }
        }

        public int Heigth
        {
            get 
            { 
                throw new NotImplementedException(); 
            }
        }

        public int Size
        {
            get 
            {
                return (int)_imageStream.Length;
            }
        }

        public string Filename
        {
            get 
            {
                return _filename;
            }
        }

        public Stream ImageStream
        {
            get 
            {
                return _imageStream;
            }

        }

        public IImage Resize(int width, int heigth, ImageResizeMode mode = ImageResizeMode.KeepRatio)
        {
            throw new NotImplementedException();
        }

        public async Task<string> ToBase64Async(ImageBase64Mode mode = ImageBase64Mode.Common)
        {
            return await Task<string>.Run(() => ToBase64(mode));
        }

        // @Public
        public Image(byte[] buffer)
        {
            _imageStream = new MemoryStream(buffer);
        }

        // @Protected
        protected MemoryStream  _imageStream = new MemoryStream();
        protected string        _filename = string.Empty;

        protected string ToBase64(ImageBase64Mode mode = ImageBase64Mode.Common)
        {
            string base64String = Convert.ToBase64String(_imageStream.ToArray());

            if (mode == ImageBase64Mode.Common)
            {
                base64String = base64String.Replace(" ", "+");
            }

            return base64String;
        }
    }
}
